package keter.framework.web.util;

import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

/**
 * <p>Class       : keter.web.util.DataSourceValidator
 * <p>Descdription: 用于校验Weblogic连接池是否可以成功连接
 *
 * @author  gulixing@msn.com
 * @version 1.0.0
 */
public class JNDIDataSourceValidator {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(JNDIDataSourceValidator.class);
	
	public static boolean validateWLS(String providerUrl, String dsName) throws SQLException {
		Context ctx = null;
		try {
			Properties prop = new Properties();
			prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
					"weblogic.jndi.WLInitialContextFactory");
			prop.setProperty(Context.PROVIDER_URL, providerUrl);// t3://192.168.0.11:7001
			ctx = new InitialContext(prop);
		} catch (NamingException ne) {
			ne.printStackTrace();
			return false;
		}
		try {
			 ctx.lookup(dsName);
		} 
		  catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		logger.info("可以连接");
		return true;
	}

}